﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IIControls.Source
{
    /// <summary>
    /// PumpCmd.xaml 的交互逻辑
    /// </summary>
    public partial class PumpCmd : UserControl
    {
        /// <summary>
        /// 初始化按钮，传入按钮类型
        /// </summary>
        /// <param name="C"></param>
        public PumpCmd(Cmd C, GetPumpButton B)
        {
            InitializeComponent();
            AddTxt("Channel1Open", "通道1开");
            AddTxt("Channel1Close", "通道1关");
            AddTxt("Channel2Open", "通道2开");
            AddTxt("Channel2Close", "通道2关");
            AddTxt("Channel3Open", "通道3开");
            AddTxt("Channel3Close", "通道3关");
            AddTxt("Channel4Open", "通道4开");
            AddTxt("Channel4Close", "通道4关");
            AddTxt("SyncTime", "同步时间");
            AddTxt("ReadTime", "读取时间");
            AddTxt("ReadRecord", "读取记录");
            AddTxt("ClearRecord", "清除记录");
            AddTxt("ValveOpen", "闸阀开");
            AddTxt("ValveClose", "闸阀关");
            ButtonType = C;
            GPB = B;
            IsEnable = false;
        }

        GetPumpButton GPB;

        /// <summary>
        /// 按钮按下发送一个点击了的按钮类型
        /// </summary>
        /// <param name="PC"></param>
        public delegate void GetPumpButton(PumpCmd PC);


        private Cmd buttontype;
        /// <summary>
        /// 记录按钮类型
        /// </summary>
        public Cmd ButtonType
        {
            get { return buttontype; }
            set
            {
                buttontype = value;
                CmdName = TxtMsg[buttontype.ToString()];
            }
        }

        /// <summary>
        /// 文字信息，为了支持多语言
        /// </summary>
        Dictionary<string, string> TxtMsg = new Dictionary<string, string>();

        /// <summary>
        /// 添加按钮文字信息，存储在字典中，keyvaluepair的形式出现
        /// </summary>
        /// <param name="txtkey">key值</param>
        /// <param name="txt">value值</param>
        public void AddTxt(string txtkey,string txt)
        {
            if (TxtMsg.ContainsKey(txtkey))
                TxtMsg[txtkey] = txt;
            else
                TxtMsg.Add(txtkey, txt);
        }




        public string CmdName
        {
            get { return (string)GetValue(CmdNameProperty); }
            set { SetValue(CmdNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CmdName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CmdNameProperty =
            DependencyProperty.Register("CmdName", typeof(string), typeof(PumpCmd ), new PropertyMetadata("泵房按钮"));



        private bool isenable;

        public bool IsEnable
        {
            get { return isenable; }
            set
            {
                isenable = value;
                if(isenable )
                    Root.Background  = new SolidColorBrush(Color.FromArgb(0xFF, 0xEF, 0x84, 0x21));
                else
                    Root.Background  =  new SolidColorBrush(Color.FromArgb(0xFF, 0x9E, 0xD8, 0xF5));
            }
        }


        /// <summary>
        /// 按钮按下时间
        /// </summary>
        public DateTime PressTime { get; set; }

        /// <summary>
        /// 按钮类型
        /// </summary>
        public  enum Cmd
        {
            /// <summary>
            /// 通道1开
            /// </summary>
            Channel1Open,
            /// <summary>
            /// 通道1关
            /// </summary>
            Channel1Close,
            /// <summary>
            /// 通道2开
            /// </summary>
            Channel2Open,
            /// <summary>
            /// 通道2关
            /// </summary>
            Channel2Close,
            /// <summary>
            /// 通道3开
            /// </summary>
            Channel3Open,
            /// <summary>
            /// 通道3关
            /// </summary>
            Channel3Close,
            /// <summary>
            /// 同步时间
            /// </summary>
            SyncTime,
            /// <summary>
            /// 读取时间
            /// </summary>
            ReadTime,
            /// <summary>
            /// 读取记录
            /// </summary>
            ReadRecord,
            /// <summary>
            /// 清楚记录
            /// </summary>
            ClearRecord,
            /// <summary>
            /// 闸阀开
            /// </summary>
            ValveOpen,
            /// <summary>
            /// 闸阀关
            /// </summary>
            ValveClose
        }

        private void Border_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleTransform ST = new ScaleTransform(1.05,1.05);
            Border B = (Border)sender;
            B.RenderTransform = ST;
            B.RenderTransformOrigin = new Point(0.5, 0.5);
            B.Cursor = Cursors.Hand;
        }

        private void Border_MouseLeave(object sender, MouseEventArgs e)
        {
            ScaleTransform ST = new ScaleTransform(1, 1);
            Border B = (Border)sender;
            B.RenderTransform = ST;
            B.Cursor = Cursors.Arrow;
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            buttonclick();
        }

        public  void buttonclick()
        {
            IsEnable = !IsEnable;
            PressTime = DateTime.Now;
            GPB(this);
        }
    }
}
